<template>
  <div>
    <div class="crumbs">
      <el-form ref="form" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="商品标题">
          <el-input v-model="formInline.keywords" placeholder="请输入……" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch('form')">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" border style="width: 100%" :row-key="'aa'">
      <el-table-column prop="id" label="ID" width="100"></el-table-column>
      <el-table-column prop="goods_title" label="商品标题"></el-table-column>
      <el-table-column prop="number" label="商品总数" width="100"></el-table-column>
      <el-table-column prop="integral" label="兑换所需积分" width="150"></el-table-column>
      <el-table-column prop="order_code" label="订单号" width="180"></el-table-column>
      <el-table-column prop="pay_time" label="冻结时间" :formatter="timeFormat" width="170"></el-table-column>
      <el-table-column prop="shipments_time" label="发货时间" :formatter="timeFormat" width="170"></el-table-column>
      <el-table-column prop="status_text" label="订单状态" width="100"></el-table-column>
      <el-table-column label="操作" width="160">
        <template slot-scope="scope">
          <el-button size="small" @click="see(scope.$index, scope.row)">查看</el-button>
          <el-button v-if="scope.row.transfer_status === 0" size="small" @click="shipments(scope.$index, scope.row)">发货</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @current-change ="handleCurrentChange"
        layout="total, prev, pager, next"
        :current-page="textData.current_page"
        :page-size="textData.per_page"
        :total="textData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import { mapState } from 'vuex'

  export default {
    data () {
      return {
        formInline: {
          keywords: ''
        }
      }
    },
    computed: {
      ...mapState({
        tableData: state => state.mallorder.list,
        textData: state => state.mallorder.pageData
      })
    },
    created () {
      this.getOrderList()
    },
    methods: {
      async getOrderList () {
        let payload = {
          page: this.textData.current_page,
          listRows: this.textData.per_page,
          keywords: this.$route.query.keywords
        }
        await this.$store.dispatch('mallorder/orderList', payload)
      },
      onSearch (formName) {
        const self = this
        self.$refs[formName].validate((valid) => {
          if (valid) {
            self.$router.push({path: '/integral/order', query: {keywords: self.formInline.keywords}})
            self.getOrderList()
          }
        })
      },
      handleCurrentChange (val) {
        this.textData.current_page = val
        this.getOrderList()
      },
      see (index, row) {
        this.$store.commit('mallorder/INTEGRAL_ORDER_SAVE_DATA', row)
        this.$router.push('/integral/orderinfo')
      },
      shipments (index, row) {
        this.$router.push({name: 'IntegralShipments', params: {id: row.id}})
      },
      timeFormat: function (value) {
        let mydate = new Date()
        mydate.setTime(value.pay_time * 1000)
        let result = mydate.getFullYear() + '-' + (mydate.getMonth() + 1) + '-' + mydate.getDate() + ' ' + mydate.getHours() + ':' + mydate.getMinutes() + ':' + mydate.getSeconds()
        return result
      }
    }
  }
</script>
